package Utils;

import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.util.Properties;
import java.util.Scanner;
import java.util.UUID;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;

/**
 * 	文件在线存储和下载
 * @author 我爱陈果果
 */
public class AliyunPicture {
	
	private Properties p = null;
	private String endpoint;
	// 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
	private String accessKeyId;
	//密钥
	private String accessKeySecret;
	//根目录
	private String bucketName;
	
	public AliyunPicture() throws Exception {
		p = getProperties("files.properties");
		endpoint = p.getProperty("endpoint");
		accessKeyId = p.getProperty("accessKeyId");
		accessKeySecret = p.getProperty("accessKeySecret");
		bucketName = p.getProperty("bucketName");
	}
	
	/**
	 * 	文件上传
	 * @param in 需要上传文件的流
	 * @param savePath 存储的路径，包括文件名
	 */
	public void uploadFile(InputStream in, String savePath) {
		// 创建OSSClient实例。操作云端数据的客户端
		OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
		//调用putObject上传文件
		ossClient.putObject(bucketName, savePath, in);
		// 关闭OSSClient。
		ossClient.shutdown();
	}
	
	/**
	 * 	下载云端文件
	 * @param out 将文件写入本地的一个输出流（也可以是response的输出流）
	 * @param dowloadPath
	 * @param savePath
	 * @throws IOException
	 */
	public void downloadFile(OutputStream out, String dowloadPath) throws IOException {
		// 创建OSSClient实例。
		OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

		// ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
		OSSObject ossObject = ossClient.getObject(bucketName, dowloadPath);
		//获取云端文件的输入流
		InputStream in = ossObject.getObjectContent();
		
		//将文件写入输出流
		int len = 0;
		byte[] b = new byte[1024];
		BufferedOutputStream bos = new BufferedOutputStream(out);
		while ((len = in.read(b)) != -1) {
			bos.write(b, 0, len);
		}
		bos.close();
		in.close();
		// 关闭OSSClient。
		ossClient.shutdown();
	}
	
	
	/**
	 * 	通过这个类的反射获取项目的根目录，最终得到一个Properties对象
	 * @param prosPath Properties文件的路径
	 * @return 加载了Properties文件的对象
	 * @throws Exception 
	 */
	public Properties getProperties(String prosPath) throws Exception {
		//1.创建一个properties对象
		//这个对象里面就有操作这个properties文件所有方法
		Properties pros = new Properties();
		//2.获取源文件的字节输入流
		InputStream inStream = null;
		try {
			//class文件所在的位置的根目录。
			String path = AliyunPicture.class.getResource("/").getPath();
			//将中文的url编码解析%ert%weq
			path = URLDecoder.decode(path, "utf-8");
			inStream = new FileInputStream(path + "\\" + prosPath);
			//3.properties对象加载源文件
			pros.load(inStream);
		} catch (FileNotFoundException e) {
			throw new FileNotFoundException("Properties文件路径未找到!");
		} catch (IOException e) {
			throw new IOException("Properties文件流加载失败!");
		}
		return pros;
	}
	
	public void endTime(String picturePath,int zc_user_id) {
		try {
			AliyunPicture fileOSSUtils = new AliyunPicture();
			SavePicture savePicture = new SavePicture();
			//文件上传
			InputStream in = new FileInputStream(picturePath);
			// 需要存到数据库
			String fileName= UUID.randomUUID()+".jpg";
			// 云端存储路径
			String savePath = "gc0910/middleProject/group6/product/"+fileName;
			fileOSSUtils.uploadFile(in, savePath);
			System.out.println("文件上传成功！");
			// 存到数据库
			savePicture.save(fileName, zc_user_id);
			
			//云端的文件夹不可能让你去遍历的
			//1.从数据库把所有要下载的地址用集合或者数组存起来
			//2.循环调用downloadFile()方法就是批量下载了
			//文件下载
//			String savePath = "G:\\81ac0880-7ac4-4c33-bf28-be9163d0e458.docx";
//			OutputStream out = new FileOutputStream(savePath);
//			String dowloadPath = "gc0910/middleProject/group6/product/81ac0880-7ac4-4c33-bf28-be9163d0e458.docx";
//			fileOSSUtils.downloadFile(out, dowloadPath);
//			System.out.println("下载成功！");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public void saveTime(String picturePath,int zc_user_id) {
		try {
			AliyunPicture fileOSSUtils = new AliyunPicture();
			SavePicture savePicture = new SavePicture();
			//文件上传
			InputStream in = new FileInputStream(picturePath);
			// 需要存到数据库
			String fileName= UUID.randomUUID()+".jpg";
			// 云端存储路径
			String savePath = "gc0910/middleProject/group6/product/"+fileName;
			fileOSSUtils.uploadFile(in, savePath);
			System.out.println("文件上传成功！");
			// 存到数据库
			savePicture.save(fileName, zc_user_id);
			
			//云端的文件夹不可能让你去遍历的
			//1.从数据库把所有要下载的地址用集合或者数组存起来
			//2.循环调用downloadFile()方法就是批量下载了
			//文件下载
//			String savePath = "G:\\81ac0880-7ac4-4c33-bf28-be9163d0e458.docx";
//			OutputStream out = new FileOutputStream(savePath);
//			String dowloadPath = "gc0910/middleProject/group6/product/81ac0880-7ac4-4c33-bf28-be9163d0e458.docx";
//			fileOSSUtils.downloadFile(out, dowloadPath);
//			System.out.println("下载成功！");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
}

